
let websocket = null;
let ip = document.location.hostname;
let wsUri = "ws://" + ip + ":8089";

const ReadyState = {0:"CONNECTING", 1:"OPEN", 2:"CLOSING", 3:"CLOSED", 4:"UNKNOWN"};
const LedStatus = {0:"none", 1:"active", 2:"flash"};
function initWebSocket() {
    try {
        if (websocket) {
            if (websocket.readyState === 1) {
                websocket.close();
            } else if (websocket.readyState !== 3) {
                console.log("checkSocket :", ReadyState[websocket.readyState]);
                return;
            }
        }

        websocket = new WebSocket(wsUri);

        websocket.onopen = function (evt) {
            isConnect = true;
            console.log("Message received : CONNECTED", evt);
        };

        websocket.onclose = function (evt) {
            isConnect = false;
            console.log("Message received : DISCONNECTED", evt);
        };

        websocket.onmessage = function (evt) {
            console.log("Message received :", evt);
            let result = JSON.parse(evt.data);
            handleResult(result);
        };

        websocket.onerror = function (evt) {
            console.log("Message error :", evt.data);
        };
    } catch (exception) {

    }
}

function sendMessage(msg) {
    if (websocket != null) {
        websocket.send(msg);
        console.log("websocket sent :", msg);
    }
}

function stopWebSocket() {
    if (websocket) websocket.close();
}

